#include <time.h>
#include <unistd.h>
#include <iostream>

using namespace std;

/*
  Function acts as a stop-watch - first call starts running, second one stops it and returns 
  elapsed msecs
*/
long TimerMSec()
{
    static timespec *ts = NULL;
    
    if (NULL==ts)
    {
	ts = new timespec;
	clock_gettime(CLOCK_REALTIME, ts);
	return 0;
    }
    
    if (ts)
    {
	timespec ts2;
	clock_gettime(CLOCK_REALTIME, &ts2);
	long res = (ts2.tv_sec - ts->tv_sec)*1000 + (ts2.tv_nsec-ts->tv_nsec)/((long)(1000000));
	delete ts;
	ts = 0;
	return res;
    }
}


int main()
{
 
 TimerMSec();
 sleep(2);
 long n = TimerMSec();
 
 cout << n
      << endl;


}
